# gameplay/cards/effects/CardEffectResolver.gd 卡牌效果解析器
class_name CardEffectResolver
var card_manager : CardManager = CardManager.new()
var shared_tween: Tween
	
#卡牌高亮效果
func highlight_card (card:CardBase , hovered):
	if hovered :
		card.scale = GlobalConstants.HIGHLIGHT_SCALE
		card.z_index =  GlobalConstants.HIGHLIGHT_ZINDEX
	else :
		card.scale = GlobalConstants.BASE_SCALE
		card.z_index =  card.collision_layer

#卡牌翻转效果
func reveal_adjacent_cards(card: CardBase) -> void:
	#通过被消除卡牌反查父节点，遍历父节点的子节点确认都被消除便设为active
	for parent in card.parents :
		var reveal :bool = true
		var children := _get_child_cards(parent)
		for child_card in children:
			if child_card == null:
				continue
			if child_card.current_state != CardBase.State.ELIMINATED:
				reveal = false
		if reveal :
			parent.current_state = CardBase.State.ACTIVE
			_create_reveal_effect(parent)
			
func _get_child_cards(parent: CardBase) -> Array[CardBase]:
	var result :Array[CardBase] 
	result = card_manager.scenic_card_hierarchy.get(parent)
	return result 

func _create_reveal_effect(card: CardBase) -> void:
	if not shared_tween:
		shared_tween = card_manager.create_tween()
	shared_tween.stop()
	shared_tween.tween_property(card, "scale", GlobalConstants.HIGHLIGHT_SCALE, 0.1)
	shared_tween.tween_property(card, "scale", GlobalConstants.BASE_SCALE, 0.1)

func animate_card_to_pos(card: CardBase,new_position: Vector2) :
	if not shared_tween:
		shared_tween = card_manager.create_tween()
	shared_tween.stop()
	shared_tween.tween_property(card, "position", new_position, 0.1)
